import HSequest from "@service/request/index";
import { BASE_URL, TIME_OUT } from "@service/request/config";
import { startsWith } from "lodash";
import { ElMessage } from "element-plus";

const hRequest = new HSequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config) => {
      const token = localStorage.getItem("token");
      if (token) {
        config.headers.Authorization = `Bearer ${token}`;
      }
      return config;
    },
    requestInterceptorCatch: (err) => {
      console.log("请求失败的拦截");
      return err;
    },
    responseInterceptor: (res) => {
      return res.data;
    },
    responseInterceptorCatch: (err) => {  
      console.log("响应失败的拦截");
      if (err.response.data.msg.includes("Token")) {
        ElMessage.error("身份过期，请重新刷新页面！");
        localStorage.removeItem("token");
      }
      return err;
    },
  },
});

export default hRequest;
